/*
 * @Date: 2022-04-24 11:37:24
 * @LastEditors: jiangh
 * @LastEditTime: 2022-04-25 09:57:01
 * @FilePath: \note\koa2-mysql-master\lib\mysql.js
 */
import mysql from "mysql2";
import config from "../config";

import Utils from "../utils/index";

// console.log(config.mysqlLogin);

/*建立连接池*/
let pool = mysql.createPool(config.mysqlLogin);

/*连接数据库*/
let allSqlAction = async (sql: any, value: any = undefined) => {
  console.log(sql, value);
  return new Promise((resolve: any, reject: any) => {
    pool.getConnection((err: any, connection: any) => {
      if (err) {
        console.log(err);
        reject(err);
      } else {
        console.log("数据库连接成功");
        connection.query(sql, value, (err: any, row: any) => {
          if (err) {
            reject(err);
            /* TODO 报错被截断 */
          } else {
            /* 全局设置返回数据为驼峰命名法 */
            resolve(Utils.convertToCamelCase(row));
          }
          connection.release();
        });
      }
    });
  });
};

export default {
  allSqlAction,
};
